<?php

namespace App\Kuafu\Shop\Controller\Merchant;

use App\Exceptions\BaseException;
use App\Http\Middleware\InternalApiAuth;
use App\Kuafu\Base\Controller\BaseController;
use App\Kuafu\Common\Enum\CodeEnum;
use App\Kuafu\Common\Service\CommonService;
use App\Kuafu\Shop\Enum\ShopCodeEnum;
use App\Kuafu\Shop\Exceptions\ShopException;
use App\Kuafu\Shop\Model\CollectShopListModel;
use App\Kuafu\Shop\Service\ShopService;
use App\Kuafu\Shop\Service\ShopStatisticsService;
use App\Kuafu\Shop\Validate\ShopValidate;
use App\Kuafu\User\Enum\RoleCodeEnum;
use App\Kuafu\User\Exceptions\AuthException;
use App\Kuafu\User\Exceptions\UserException;
use App\Kuafu\User\Service\AuthService;
use GuzzleHttp\Exception\GuzzleException;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Log;

class ShopStatisticsController extends BaseController
{
    /**
     * 门店风云榜
     *
     * @param Request $request
     * @return JsonResponse
     * @throws UserException
     * @throws ShopException|AuthException
     */
    public function getWindCloudRank(Request $request): JsonResponse
    {
        // 不同路由获取
        $action     = request()->route()->getAction();
        $middleware = attributeGet($action, "middleware", []);
        if (!in_array(InternalApiAuth::class, $middleware)) {
            $payload = AuthService::getPayload()->toArray();
            $isStaff = isset($payload["thirdLoginType"]) && $payload["thirdLoginType"];
            if (!$isStaff) {
                return $this->fail(ShopCodeEnum::ERR_NO_ACCESS_ERROR);
            }
        }
        $startAt    = $request->get('startAt', time());
        $endAt      = $request->get('endAt', time());
        $sortColumn = $request->get('sortColumn', '');
        $sort       = $request->get('sort', '');
        $page       = $request->get('pageNum', 0);
        $pageSize   = $request->get('pageSize', 10);
        $shopName   = (string)$request->get('shopName', '');
        $city       = (string)$request->get('city', '');
        $province   = (string)$request->get('province', '');
        $district   = (string)$request->get('district', '');
        $branch     = (string)$request->get('branch', '');

        // 从运营APP查询
        $kmsUserId     = (int)$request->get('kmsUserId', 0);
        $provinceCodes = (string)$request->get('provinceCodes', '');
        $cityCodes     = (string)$request->get('cityCodes', '');
        $areaCodes     = (string)$request->get('areaCodes', '');
        $shopIds       = (string)$request->get('shopIds', '');
//        $startAt = 1681747200;
//        $endAt = 1681833599;
        $isKms = false;
        if ($kmsUserId) {
            $userId = $kmsUserId;
            $isKms  = true;// kms用户通过省市区code查询
        } else {
            $userId = AuthService::getUserId(false);
        }
        $valid  = ShopValidate::getShopList(['userId' => $userId]);
        if ($valid) {
            return $this->fail(ShopCodeEnum::ERR_SHOP_INPUT, $valid);
        }
        $provinceCodes = $provinceCodes ? explode(',', $provinceCodes) : [];
        $cityCodes     = $cityCodes ? explode(',', $cityCodes) : [];
        $areaCodes     = $areaCodes ? explode(',', $areaCodes) : [];
        $shopIds       = $shopIds ? explode(',', $shopIds) : [];

        return $this->success(
            (new ShopStatisticsService)
                ->setDate([
                    'startAt' => date('Y-m-d', $startAt),
                    'endAt'   => date('Y-m-d', $endAt),
                ])
                ->setOrderBy($sortColumn, $sort)
                ->setPage($page, $pageSize)
                ->checkUserStatus($kmsUserId)
                ->setShopList($shopName, $city, $province, $district, $branch, $userId, $isKms, $provinceCodes,
                    $cityCodes, $areaCodes, $shopIds)
                ->getWindCloud()
        );
    }

}
